const { Sequelize } = require('sequelize');
const configFn = require('./config/config.default.js');
const appConfig = configFn({ name: 'lingxi-system' });

// 创建数据库连接
const sequelize = new Sequelize(appConfig.sequelize.database, appConfig.sequelize.username, appConfig.sequelize.password, {
  host: appConfig.sequelize.host,
  port: appConfig.sequelize.port,
  dialect: appConfig.sequelize.dialect,
  timezone: appConfig.sequelize.timezone,
  define: appConfig.sequelize.define,
  logging: false, // 关闭SQL日志
});

async function checkAllOrders() {
  try {
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 查询所有销售订单
    console.log('\n=== 所有销售订单 ===');
    const orders = await sequelize.query('SELECT * FROM sales_orders ORDER BY created_at DESC', { type: sequelize.QueryTypes.SELECT });
    console.log('销售订单总数:', orders.length);
    
    orders.forEach((order, index) => {
      console.log(`订单 ${index + 1}:`);
      console.log(`  ID: ${order.id}`);
      console.log(`  平台订单ID: ${order.platform_order_id}`);
      console.log(`  客户ID: ${order.customer_id}`);
      console.log(`  订单金额: ${order.order_amount}`);
      console.log(`  订单状态: ${order.order_status}`);
      console.log(`  创建时间: ${order.created_at}`);
      console.log('---');
    });
    
    // 查询所有销售订单项
    console.log('\n=== 所有销售订单项 ===');
    const orderItems = await sequelize.query('SELECT * FROM sales_order_items ORDER BY created_at DESC', { type: sequelize.QueryTypes.SELECT });
    console.log('销售订单项总数:', orderItems.length);
    
    orderItems.forEach((item, index) => {
      console.log(`订单项 ${index + 1}:`);
      console.log(`  ID: ${item.id}`);
      console.log(`  订单ID: ${item.order_id}`);
      console.log(`  产品ID: ${item.product_id}`);
      console.log(`  产品名称: ${item.product_name}`);
      console.log(`  数量: ${item.quantity}`);
      console.log(`  单价: ${item.unit_price}`);
      console.log(`  金额: ${item.amount}`);
      console.log(`  创建时间: ${item.created_at}`);
      console.log('---');
    });
    
  } catch (error) {
    console.error('查询数据时出错:', error);
  } finally {
    await sequelize.close();
  }
}

checkAllOrders();